import { RoleId } from '@/types/role.d'
import useUserStore from '@/store/modules/user'
import { request, CResponse, ResponseCode } from '@/api/request'
import { FUNDFLOW_LIST, FUNDFLOW_UPDATE } from '@/api/flow'
import { Flow } from '@/api/flow/type'

const ManagerStatus = "'1','3'"
const FinanceStatus = "'2','4','5'"

export const useWorker = () => {
  const { info } = useUserStore()
  const visible = ref<boolean>(false)
  const dataList = ref<Flow[]>([])

  const getDataList = async () => {
    const res = await request.Post<CResponse>(FUNDFLOW_LIST, {
      status: info?.userRole === RoleId.Finance ? FinanceStatus : info?.userRole === RoleId.Manager ? ManagerStatus : '',
    }).then((res: Response) => res.json())
    if (res.code === ResponseCode.SUCCESS) {
      dataList.value = res.body
    }
  }

  const updateFlow = async (flow: Flow) => {
    const { code, body } = await request
      .Post<Response>(FUNDFLOW_UPDATE, flow)
      .then((res) => res.json())
    if (code === ResponseCode.SUCCESS && body) {
      getDataList()
    }
  }

  const addFlow = () => {
    visible.value = true
  }

  onMounted(() => getDataList())

  return {
    visible,
    dataList,
    getDataList,
    updateFlow,
    addFlow,
  }
}
